--[[
pbInfo - Includes/inc.ChatFrame.lua
	v0.41
	by p.b. a.k.a. novayuna
	released under the Creative Commons License By-Nc-Sa: http://creativecommons.org/licenses/by-nc-sa/3.0/
]]
function pbInfo.ChatFrame.AddMessage(this, message, r, g, b)
	if (not pbIsEmpty(message) and type(message) ~= "nil") then
		if (pbInfoSettings["CHATFRAMEHIDEXPTP"] and (message:find(pbInfo.ChatFrame.XPTP[1]) or message:find(pbInfo.ChatFrame.XPTP[2]))) then
			return;
		end;
		if (pbInfoSettings["CHATFRAMEHIDEXPTPDEBT"] and (message:find(pbInfo.ChatFrame.XPTPDebt[1]) or message:find(pbInfo.ChatFrame.XPTPDebt[2]))) then
			return;
		end;
		if (pbInfoSettings["CHATFRAMEFILTERTITLES"]) then
			for i, v in pairs(pbInfo.ChatFrame.FilterStrings) do
				v = v:match("^(.+%s).-:"):gsub("%[(.+)%]", "%%[%1%%]");
				if (not pbIsEmpty(v) and message:find(v) == 1) then
					message = message:gsub(v, "");
					break;
				end;
			end;
		end;
		if (pbInfoSettings["CHATFRAMESHOWPLAYERINFO"]) then
			local main, sub = "|cffffffff--|r", "|cffffffff--|r";
			local pattern = "%[(.-)%].-:.+";
			if (type(message:find("%]%s.-" .. pattern)) ~= "nil") then
				pattern = "%]%s.-" .. pattern;
			end;
			name = message:match(pattern);
			if (not pbIsEmpty(name)) then
				if (type(pbInfo.PlayerDB.Players[pbInfo.RealmName][name]) == "table") then
					local player = pbInfo.PlayerDB.Players[pbInfo.RealmName][name];
					local mColor = (type(pbInfo.Classes[player.mC]) == "table" and pbInfo.Classes[player.mC]["color"]) or "";
					main = pbColor((player.mL < 10 and "0" .. player.mL) or player.mL, mColor);
					if (player.sL > 0 and player.sC ~= "") then
						local sColor = (type(pbInfo.Classes[player.sC]) == "table" and pbInfo.Classes[player.sC]["color"]) or "";
						sub = pbColor((player.sL < 10 and "0" .. player.sL) or player.sL, sColor);
					end;
					message = "[" .. main .. "/" .. sub .. "] " .. message;
				end;
			end;
		end;
		if (pbInfoSettings["TIMESTAMP"]
			and not pbIsEmpty(message)
			and not (pbInfoSettings["TIMESTAMPNOTINCOMBATLOG"]
				and this:GetID() == 2
			)
		) then
			message = os.date("[|cffffffff" .. pbInfoSettings["TimeStampFormat"]:gsub(":", "|r:|cffffffff") .. "|r] "):lower() .. message;
		end;
	end;
	this:pbInfoChatFrame_OriginalAddMessage(message, r, g, b);
end;

function pbInfo.ChatFrame.Scripts.OnLoad()
	-- thx to Alleris for the idea!
	local newOptions = {{"EMOTE", "Emote"},{"GM", "GM"},{"RAID", "Raid"},{"YELL", "World"},{"LOOT", "Loot"},{"SYSTEM", "System"}};
	if (#DEFAULT_CHAT_CHANNEL == 6) then
		for i = 1, #newOptions, 1  do
			DEFAULT_CHAT_CHANNEL[#DEFAULT_CHAT_CHANNEL + 1] = newOptions[i][1];
			_G["MESSAGE_" .. newOptions[i][1]] = newOptions[i][2];
		end;
	end;
end;

ITEM_QUALITY0_DESC = "White";
UnitPopupButtons["ITEM_QUALITY0_DESC"] = { text = ITEM_QUALITY0_DESC };
UnitPopupMenus["LOOT_THRESHOLD"][0] = "ITEM_QUALITY0_DESC";

pbInfo.Hooked.ChatFrame_OnEvent = ChatFrame_OnEvent;
function ChatFrame_OnEvent(this, event)
    if (not this.pbInfoChatFrame_OriginalAddMessage) then
        this.pbInfoChatFrame_OriginalAddMessage = this.AddMessage;
        this.AddMessage = pbInfo.ChatFrame.AddMessage;
    end;
    pbInfo.Hooked.ChatFrame_OnEvent(this, event);
end;